﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class ModificationReposity : Reposity
    {
        public List<Apply> ListApplies(Pagination pagination)
        {
            var applies = from a in _dataContext.Modification_applies
                          from u in _dataContext.Users
                          from f in _dataContext.Factories
                          where u.FactoryId == f.Id && a.applier == u.Id && (a.status == 1 || a.status == 2)
                          orderby a.date descending
                          select new Apply
                          {
                              FactoryName = f.Name,
                              Applier = u.RealName + "(" + u.Name + ")",
                              Title = a.title,
                              Content = a.content,
                              Status = GetStatus(a.status),
                              Reason = a.reason,
                              Date = a.date,
                              Id = a.id,
                              File = a.file
                          };

            return Pagination<Apply>(applies, pagination);
        }

        private string GetStatus(int status)
        {
            if (status == 0)
            {
                return "等待审批";
            }
            else if (status == 1)
            {
                return "已批准";
            }
            else if (status == 2)
            {
                return "已处理";
            }
            else if (status == -1)
            {
                return "已拒绝";
            }

            return "";
        }

        public List<Modification_apply> ListOwnedApplies(long userId, Pagination pagination)
        {
            var applies = from a in _dataContext.Modification_applies
                          where a.applier == userId orderby a.date descending
                          select a;

            return Pagination<Modification_apply>(applies, pagination);
        }

        public List<Apply> ListFactoryApplies(long factoryId, Pagination pagination)
        {
            var applies = from a in _dataContext.Modification_applies
                          from u in _dataContext.Users
                          from f in _dataContext.Factories
                          where u.FactoryId == f.Id && a.applier == u.Id && u.FactoryId == factoryId
                          orderby a.date descending
                          select new Apply
                          {
                              FactoryName = f.Name,
                              Applier = u.RealName + "(" + u.Name + ")",
                              Title = a.title,
                              Content = a.content,
                              Status = GetStatus(a.status),
                              Reason = a.reason,
                              Date = a.date,
                              Id = a.id,
                              File = a.file
                          };

            return Pagination<Apply>(applies, pagination);
        }

        public Modification_apply GetApplyById(long id)
        {
            var apply = from a in _dataContext.Modification_applies
                          where id == a.id
                          select a;

            return apply.First();
        }

        public int GetApplyCountByStatus(long factoryId, long applierId, int status)
        {
            var applies = from a in _dataContext.Modification_applies
                          from u in _dataContext.Users
                          from f in _dataContext.Factories
                          where (factoryId <= 0 || u.FactoryId == factoryId) && (applierId <= 0 || a.applier == applierId) && a.status == status && u.FactoryId == f.Id && a.applier == u.Id
                          select new Apply
                          {
                              FactoryName = f.Name,
                              Applier = u.RealName + "(" + u.Name + ")",
                              Title = a.title,
                              Content = a.content,
                              Status = GetStatus(a.status),
                              Reason = a.reason,
                              Date = a.date,
                              Id = a.id,
                              File = a.file
                          };

            return applies == null ? 0 : applies.Count();
        }

        public Apply GetDetailById(long id)
        {
            var apply = from a in _dataContext.Modification_applies
                        from u in _dataContext.Users
                        from f in _dataContext.Factories
                        where u.FactoryId == f.Id && a.applier == u.Id && a.id == id
                        orderby a.date descending
                        select new Apply
                        {
                            FactoryName = f.Name,
                            Applier = u.RealName + "(" + u.Name + ")",
                            Title = a.title,
                            Content = a.content,
                            Status = GetStatus(a.status),
                            Reason = a.reason,
                            Date = a.date,
                            Id = a.id,
                            File = a.file,
                            type = a.type
                        };

            return apply.First();
        }

        public Modification_apply CreateApply(Modification_apply apply)
        {
            apply.date = DateTime.Now;
            _dataContext.Modification_applies.InsertOnSubmit(apply);
            SubmitChanges();
            return apply;
        }

        public bool DeleteApply(Modification_apply apply)
        {
            _dataContext.Modification_applies.DeleteOnSubmit(apply);
            SubmitChanges();
            return true;
        }

        public Modification_apply EditApply(Modification_apply apply)
        {
            SubmitChanges();
            return apply;
        }
    }
}